package photobox.manage

import grails.converters.JSON
import photobox.BaseController
import photobox.exception.NotFoundException
import photobox.utils.ParamUtils
import photobox.utils.ResultUtil
import photobox.photo.Category

class MngCategoryController extends BaseController{
    def  categoryService
    def index() {}
    def create() {
        def typeList = [
                [id:Category.CATEGORY_TYPE_THEME,text:'主题'],
                [id:Category.CATEGORY_TYPE_STYLE,text:'风格']
        ]
        [typeList:typeList]
    }
    def list() {
        def searchParams = ParamUtils.parseFilter(params.filters)
        def pageParams = ParamUtils.parsePage(params)
        def sortParams = ParamUtils.parseSort(params,
                ["id", "name", "remark","categoryType"], [sort: 'id', order: "desc"])
        def userRs = categoryService.listCategory(searchParams, pageParams, sortParams)
        render ResultUtil.pageResults(userRs.rs, userRs.total, pageParams,
                [id: 'id', name: 'name', remark: 'remark',categoryType:'categoryType']) as JSON
    }
    def save() {
        categoryService.createCategory(params)
        render([error: 0, message: "创建图片分类完成"] as JSON)
    }
    def edit() {
        def typeList = [
                [id:Category.CATEGORY_TYPE_THEME,text:'主题'],
                [id:Category.CATEGORY_TYPE_STYLE,text:'风格']
        ]
        def category = Category.get(params.id as Long)
        if (!category) {
            throw new NotFoundException("没有找到对应的图片分类")
        }
        return [category: category,typeList:typeList]
    }

    def update() {
        categoryService.updateCategory(params)
        render([error: 0, message: "更新图片分类完成"] as JSON)
    }

    def delete() {
        categoryService.deleteCategory(params)
        render([error:0, message: "删除图片分类完成"] as JSON)
    }
}
